package com.lminkey.servlet;

import com.lmonkey.dao.MD5;
import com.lmonkey.servic.ADDRESSDao;
import com.lmonkey.servic.USERDao;
import com.lmonkey.shop.USER;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.NoSuchAlgorithmException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

@WebServlet("/admin/admin_douseradd")
public class douseradd extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //设置字符集
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");

        final String JDBC_DRIVER = "com.mysql.cj.jdbc.Driver";
        final String DB_URL = "jdbc:mysql://localhost:3306/lmonkeyshop?useSSL=false&allowPublicKeyRetrieval=true&serverTimezone=UTC";
        final String USER = "root";
        final String PASS = "root";

        //获取参数(从admin_useradd.jsp)
        String username = request.getParameter("username");
        String name = request.getParameter("name");
        String pass = request.getParameter("password");
        String sex = request.getParameter("sex");
        String birthday = request.getParameter("birthday");
        String email = request.getParameter("email");
        String mobile = request.getParameter("mobile");
        String address = request.getParameter("address");
        //创建用户实体
        USER u = new USER(username,name, pass, sex, birthday, null, email, mobile, address, 1);

        //加入到数据库中
        String sql = "insert into lmonkeyshop values(?,?,?,?,DATE_FORMAT(?,'%Y-%m-%d'),?,?,?,?,?)";
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //注册 jdbc 驱动
            Class.forName(JDBC_DRIVER);
            //打开数据库连接
            conn = DriverManager.getConnection(DB_URL, USER, PASS);
        }catch(SQLException e)
        {
            e.printStackTrace();
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
        int count = 0;


        try{
            String pass1 = MD5.md5(u.getuser_pass());
            ps = conn.prepareStatement(sql);
            ps.setObject(1,u.getuser_id());
            ps.setObject(2,u.getuser_name());
            ps.setObject(3,pass1);
            ps.setObject(4,u.getuser_sex());
            ps.setObject(5,u.getuser_birthday());
            ps.setObject(6,u.getuser_idenity_code());
            ps.setObject(7,u.getuser_email());
            ps.setObject(8,u.getuser_mobile());
            ps.setObject(9,u.getuser_address());
            ps.setObject(10,u.getuser_status());

            count = ps.executeUpdate();
        }catch (SQLException e)
        {
            e.printStackTrace();
        }
        //进行判断
        if (count>0){
            String filename = "tx.png";
            //将头像写入数据库
            USERDao.inserPhoto(filename,username);
            //将地址写入数据库
            ADDRESSDao.add(name,username,mobile,address);
            response.sendRedirect("/lyq/admin/admin_douserselect");
        }else{
            PrintWriter out = response.getWriter();
            out.write("<script>");
            out.write("alert('用户添加失败')");
            out.write("location.href='admin_useradd");
            out.write("</script>");
        }
    }
}
